// pages/forumAdd/forumAdd.js
var WxNotificationCenter = require("../../config/WxNotificationCenter");
import forum from '../../config/api/forum' // 引入api接口管理文件
import {
  baseUrl
} from '../../config/config';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pictureList: [],
    name: "",
    content: "",
    anonymousArr: ["否","是"],
    anonymous: 0,
  },
  afterRead(event){
    let that = this;
    wx.showLoading({
      title: '上传中...'
    });

    let url = baseUrl + 'aliyunFile/uploadFile';
    wx.uploadFile({
      url: url, //仅为示例，非真实的接口地址
      filePath: event.detail.file.url,
      name: 'file',
      success(res) {
        const data = res.data
        let urlpath = JSON.parse(data)["data"];
        const file = event.detail.file;
        file.imgUrl = urlpath
        var jsonlist = that.data.pictureList.concat(file);
        that.setData({
          pictureList: jsonlist
        })
        wx.hideLoading();
        console.log(that.data)
      },
      fail(error) {
        console.log(error);
      },
      complete() {
        console.log("complete");
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })


    // const file = event.detail.file;
    // var jsonlist = this.data.pictureList.concat(file);
    // this.setData({
    //   pictureList: jsonlist
    // })
    // wx.hideLoading();
    // console.log(this.data)
  },
  //处理文本双向绑定问题
  onChange(e) {
    this.setData({
      [e.currentTarget.dataset.prop]: e.detail
    })
  },
  //处理带数组类型的绑定
  moretypechange(e) {
    let arr = this.data[e.currentTarget.dataset.arrdata];
    this.setData({
      [e.currentTarget.dataset.prop]: e.detail.value
    })
  },
  deleteClick(event) {
    var pictureList = this.data.pictureList;
    pictureList.splice(event.detail.index, 1)
    this.setData({
      pictureList: pictureList
    })
  },

  clicktijiao() {
    if (this.data.name.length == 0) {
      wx.showToast({
        title: '请输入标题',
        icon: "none",
      })
      return;
    }
    if (this.data.name.price == 0) {
      wx.showToast({
        title: '请输入帖子内容',
        icon: "none",
      })
      return;
    }
    //分割字符串

    wx.showLoading({
      title: '加载中...',
      mask: true,
    })
    forum.forumSave({
      "name": this.data.name,
      "creater": "1563458454352408581",
      "anonymous": this.data.anonymous,
      "content": this.data.content,
      "pictureList": this.data.pictureList
    }, function (res) {
      wx.hideLoading({
        success: (res) => {},
      })
      if (res.data.status == 200) {
        wx.showModal({
          title: '添加成功',
          showCancel: false,
          success: (result) => {},
          fail: (res) => {},
          complete: (res) => {
            wx.navigateBack({
              delta: 1,
            })
          },
        })
        WxNotificationCenter.postNotificationName("addForum");
      }
    })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})